package com.shyroke.daydayzhuansvc.service;

import com.aliyun.oss.common.utils.DateUtil;
import com.jcraft.jsch.UserInfo;
import com.shyroke.daydayzhuansvc.util.CommonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;
import java.util.Date;

/**
 * 类名 ：
 * 用法 ：
 * 创建人 ： shyroke
 * 时间：2018/10/25 17:21
 */
@Component
public class MailService {

    private static String pre = "<!DOCTYPE html>"
            + "<html>"
            + "<head>"
            + "<title>天天线报网</title>"
            + "<meta name=\"content-type\" content=\"text/html; charset=UTF-8\">"
            + "</head>"
            + "<body>";

    private static String newSubmitArt = "<p>有新的投稿文章，请及时处理</p>";

    private static String newSubmitArtSuccess = "<p>您的投稿文章：#1,审核通过！</p>";
    private static String newSubmitArtFail = "<p>您的投稿文章：#1,审核失败，失败原因：#2</p>";

    private static String newSubmitArt1 = "<p>亲爱的<font style=\"color:red\">#1</font>,今天(#2)待做的事情是：<font style=\"color:red\">#3</font></p>";

    private static String suff = "</body>"+ "</html>";

    /**
     * 回复
     */
    private static String newReply = "<p>您的评论有了新的回复，点击链接进入文章查看回复内容：<a href='#2'>文章链接</a></p>";



    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private JavaMailSender mailSender;

    /**
     * 新投稿提醒
     */
    public void sendNewSubmitMail(){

        try {

            MimeMessage mimeMessage = this.mailSender.createMimeMessage();
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);

            message.setFrom("865386512@qq.com");
            message.setTo("qweyhj@qq.com");//设置收件人
            message.setSubject("新投稿提醒");

            message.setText(pre+newSubmitArt+suff,true);

            this.mailSender.send(mimeMessage);

        }catch (Exception e){
            logger.info("发送邮件异常",e);
        }

    }

    /**
     * 新回复邮件提示
     * @param email
     * @param projectUrl
     */
    public void sendNewReply(String email, String projectUrl) {

        if(!CommonUtil.isEmail(email)){
            return;
        }

        try {

            MimeMessage mimeMessage = this.mailSender.createMimeMessage();
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);

            message.setFrom("865386512@qq.com");
            message.setTo(email);//设置收件人
            message.setSubject("天天线报网:您有新的回复请及时查看");

            String newContent = pre+newReply+suff;
            newContent = newContent.replaceAll("#2",projectUrl);

            message.setText(newContent,true);

            this.mailSender.send(mimeMessage);

        }catch (Exception e){
            logger.info("发送邮件异常",e);
        }

    }

    /**
     * 投稿审核成功或者失败
     */
    public void sendNewProjectSuccess(String  email,String title) {
        try {

            MimeMessage mimeMessage = this.mailSender.createMimeMessage();
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);

            message.setFrom("865386512@qq.com");
            message.setTo(email);//设置收件人
            message.setSubject("天天线报网:投稿审核成功啦！");
            newSubmitArtSuccess = newSubmitArtSuccess.replaceAll("#1",title);
            String newContent = pre+newSubmitArtSuccess+suff;

            message.setText(newContent,true);

            this.mailSender.send(mimeMessage);

        }catch (Exception e){
            logger.info("发送邮件异常",e);
        }
    }

    /**
     * 投稿审核失败
     */
    public void sendNewProjectFail(String email,String title,String memo) {
        try {

            MimeMessage mimeMessage = this.mailSender.createMimeMessage();
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);

            message.setFrom("865386512@qq.com");
            message.setTo(email);//设置收件人
            message.setSubject("天天线报网:投稿审核失败！");
            newSubmitArtFail = newSubmitArtFail.replaceAll("#1",title);
            newSubmitArtFail = newSubmitArtFail.replaceAll("#2",memo);
            String newContent = pre+newSubmitArtFail+suff;

            message.setText(newContent,true);

            this.mailSender.send(mimeMessage);

        }catch (Exception e){
            logger.info("发送邮件异常",e);
        }
    }
}
